package com.cys.markdown.doc.project.service;

import com.cys.common.util.base.IdWorker;
import com.cys.common.util.base.StringUtil;
import com.cys.markdown.doc.project.interfaces.dao.ProjectRelUserDao;
import com.cys.markdown.doc.project.interfaces.entity.IProjectRelUser;
import com.cys.markdown.doc.project.entity.ProjectRelUser;
import com.cys.markdown.doc.project.interfaces.service.ProjectRelUserService;
import com.cys.mid.core.system.interfaces.entity.ISysUser;
import com.cys.mid.core.system.interfaces.service.SysUserService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.cys.common.util.base.MapUtil;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import javax.annotation.Resource;

/**
 * T_PROJECT_REL_USER【项目用户关联表】 Service接口实现类
 *
 * @author caiyushen
 */
@Service("projectRelUserServiceImpl")
public class ProjectRelUserServiceImpl implements ProjectRelUserService {

    private static Logger logger = LoggerFactory.getLogger(ProjectRelUserServiceImpl.class);

    @Autowired
    private ProjectRelUserDao projectRelUserDao;

    /**
     * 新增ProjectRelUser
     *
     * @param params ProjectRelUser实体类对应参数
     */
    @Override
    public IProjectRelUser save(Map<String, Object> params) throws Exception {
        if (!StringUtil.checkObj(params)) {
            logger.error("method[save] params is null.");
            return null;
        }
        if (!StringUtil.checkObj(params.get("projectInfoId"))) {
            logger.error("method[save] projectInfoId is null.");
            return null;
        }
        if (!StringUtil.checkObj(params.get("sysUserId"))) {
            logger.error("method[save] sysUserId is null.");
            return null;
        }
        IProjectRelUser projectRelUser = (IProjectRelUser) MapUtil.toObject(ProjectRelUser.class, params);
        projectRelUser.setProjectRelUserId(IdWorker.getId());
        projectRelUserDao.save(projectRelUser);
        return projectRelUser;
    }

    /**
     * 新增ProjectRelUser
     *
     * @param projectRelUser ProjectRelUser实体类对应参数
     */
    @Override
    public IProjectRelUser save(IProjectRelUser projectRelUser) {
        if (!StringUtil.checkObj(projectRelUser)) {
            logger.error("method[save] projectRelUser is null.");
            return null;
        }
        if (!StringUtil.checkObj(projectRelUser.getProjectInfoId())) {
            logger.error("method[save] projectInfoId is null.");
            return null;
        }
        if (!StringUtil.checkObj(projectRelUser.getSysUserId())) {
            logger.error("method[save] sysUserId is null.");
            return null;
        }
        projectRelUser.setProjectRelUserId(IdWorker.getId());
        projectRelUserDao.save(projectRelUser);
        return projectRelUser;
    }

    /**
     * 批量新增
     *
     * @param dataList
     */
    @Override
    public void saveBatch(List<IProjectRelUser> dataList) {
        if (CollectionUtils.isEmpty(dataList)) {
            return;
        }
        for (IProjectRelUser projectRelUser : dataList) {
            projectRelUser.setProjectRelUserId(IdWorker.getId());
            projectRelUserDao.save(projectRelUser);
        }
    }

    /**
     * 批量关联ProjectRelUser
     *
     * @param projectInfoIdList 关联项目主键
     * @param sysUserId         用户主键
     */
    @Override
    public void saveBatchRelUser(List<Long> projectInfoIdList, Long sysUserId) {
        if (CollectionUtils.isEmpty(projectInfoIdList)) {
            logger.warn("method[saveBatchRelUser] projectInfoIdList is null.");
            return;
        }
        if (!StringUtil.checkObj(sysUserId)) {
            logger.warn("method[saveBatchRelUser] sysUserId is null.");
            return;
        }
        //先根据sysUserId删除之前的关联
        projectRelUserDao.deleteBySysUserId(sysUserId);
        List<IProjectRelUser> insertList = new ArrayList<>();
        for (Long projectInfoId : projectInfoIdList) {
            if (StringUtil.checkObj(projectInfoId)) {
                IProjectRelUser projectRelUser = new ProjectRelUser();
                projectRelUser.setProjectRelUserId(IdWorker.getId());
                projectRelUser.setSysUserId(sysUserId);
                projectRelUser.setProjectInfoId(projectInfoId);
                insertList.add(projectRelUser);
            }
        }
        //批量新增
        this.saveBatch(insertList);
    }

    /**
     * 批量关联ProjectRelUser
     *
     * @param projectInfoId 关联项目主键
     * @param sysUserIdList 用户主键列表
     */
    @Override
    public void saveBatchRelProject(Long projectInfoId, List<Long> sysUserIdList) {
        if (CollectionUtils.isEmpty(sysUserIdList)) {
            logger.warn("method[saveBatchRelProject] sysUserIdList is null.");
            return;
        }
        if (!StringUtil.checkObj(projectInfoId)) {
            logger.warn("method[saveBatchRelProject] projectInfoId is null.");
            return;
        }
        //批量新增
        for (Long sysUserId : sysUserIdList) {
            if (StringUtil.checkObj(projectInfoId)) {
                IProjectRelUser projectRelUser = new ProjectRelUser();
                projectRelUser.setProjectRelUserId(IdWorker.getId());
                projectRelUser.setSysUserId(sysUserId);
                projectRelUser.setProjectInfoId(projectInfoId);
                projectRelUserDao.save(projectRelUser);
            }
        }
    }

    /**
     * 修改ProjectRelUser
     *
     * @param params ProjectRelUser实体类对应参数
     */
    @Override
    public IProjectRelUser update(Map<String, Object> params) throws Exception {
        if (!StringUtil.checkObj(params)) {
            logger.error("method[update] params is null.");
            return null;
        }
        Long id = MapUtil.getLong(params, "projectRelUserId");
        if (!StringUtil.checkObj(id)) {
            logger.error("method[update] params.id is null.");
            return null;
        }
        if (!StringUtil.checkObj(params.get("projectInfoId"))) {
            logger.error("method[update] projectInfoId is null.");
            return null;
        }
        if (!StringUtil.checkObj(params.get("sysUserId"))) {
            logger.error("method[update] sysUserId is null.");
            return null;
        }
        IProjectRelUser projectRelUser = (IProjectRelUser) MapUtil.toObject(ProjectRelUser.class, params);
        projectRelUserDao.update(projectRelUser);
        return projectRelUser;
    }

    /**
     * 通过id物理删除ProjectRelUser
     *
     * @param id ProjectRelUser主键
     */
    @Override
    public void deleteById(Long id) {
        if (!StringUtil.checkObj(id)) {
            logger.warn("method[deleteById] id is null.");
            return;
        }
        projectRelUserDao.deleteById(id);
    }

    /**
     * 批量物理删除ProjectRelUser
     *
     * @param ids ProjectRelUser主键数组
     */
    @Override
    public void deleteBatch(Long[] ids) {
        if (!StringUtil.checkObj(ids)) {
            logger.warn("method[deleteBatch] ids is null.");
            return;
        }
        projectRelUserDao.deleteBatch(ids);
    }

    /**
     * 根据id加载ProjectRelUser
     *
     * @param id ProjectRelUser主键
     */
    @Override
    public IProjectRelUser find(Long id) {
        if (!StringUtil.checkObj(id)) {
            logger.warn("method[find] id is null.");
            return null;
        }
        return projectRelUserDao.findEntityById(id);
    }

    /**
     * 根据用户主键/项目主键 加载ProjectRelUser
     *
     * @param sysUserId     用户主键
     * @param projectInfoId 项目主键
     * @return ProjectRelUser实体类
     */
    @Override
    public IProjectRelUser find(Long sysUserId, Long projectInfoId) {
        if (!StringUtil.checkObj(sysUserId)) {
            logger.warn("method[find] sysUserId is null.");
            return null;
        }
        if (!StringUtil.checkObj(projectInfoId)) {
            logger.warn("method[find] projectInfoId is null.");
            return null;
        }
        Map map = new HashMap();
        map.put("sysUserId", sysUserId);
        map.put("projectInfoId", projectInfoId);
        return projectRelUserDao.findEntityByUserIdAndProId(map);
    }

    /**
     * 获取ProjectRelUser列表数据
     *
     * @param map 默认空数据集
     */
    @Override
    public List<IProjectRelUser> findList(Map<String, Object> map) {
        return (List<IProjectRelUser>) projectRelUserDao.findList(map);
    }


    /**
     * 分页获取数据
     *
     * @param pageNo   当前页
     * @param pageSize 当前页数
     * @param params   检索参数
     */
    @Override
    public PageInfo<IProjectRelUser> findPage(Integer pageNo, Integer pageSize, Map<String, Object> params) {
        if (!StringUtil.checkObj(pageNo)) {
            logger.warn("method[findPage] pageNo is null. pageNo = 1");
            pageNo = 1;
        }
        if (!StringUtil.checkObj(pageSize)) {
            logger.warn("method[findPage] pageSize is null. pageSize = 5");
            pageSize = 5;
        }
        PageHelper.startPage(pageNo, pageSize);
        List<IProjectRelUser> dataList = projectRelUserDao.findPage(params);
        PageInfo<IProjectRelUser> pageInfo = new PageInfo<>(dataList);
        return pageInfo;
    }

    /**
     * 获取ProjectRelUser列表数据
     *
     * @param projectInfoId 项目信息主键
     * @return 数据列表
     */
    @Override
    public List<ISysUser> findUserListByProjectInfoId(Long projectInfoId) {
        if (!StringUtil.checkObj(projectInfoId)) {
            logger.warn("method[findUserListByProjectInfoId] projectInfoId is null.");
            return null;
        }
        Map params = new HashMap();
        params.put("projectInfoId", projectInfoId);
        return projectRelUserDao.findUserListByProjectInfoId(params);
    }

    /**
     * 获取ProjectRelUser列表数据
     *
     * @param projectInfoId 项目信息主键
     * @param sysUserName   用户名称
     * @return 数据列表
     */
    @Override
    public PageInfo<ISysUser> findUserPageByProjectInfoId(Integer pageNo, Integer pageSize, Long projectInfoId, String sysUserName) {
        if (!StringUtil.checkObj(projectInfoId)) {
            logger.warn("method[findUserListByProjectInfoId] projectInfoId is null.");
            return null;
        }
        if (!StringUtil.checkObj(pageNo)) {
            logger.warn("method[findUserPageByProjectInfoId] pageNo is null. pageNo = 1");
            pageNo = 1;
        }
        if (!StringUtil.checkObj(pageSize)) {
            logger.warn("method[findUserPageByProjectInfoId] pageSize is null. pageSize = 5");
            pageSize = 5;
        }
        Map params = new HashMap();
        params.put("projectInfoId", projectInfoId);
        params.put("sysUserName", sysUserName);
        PageHelper.startPage(pageNo, pageSize);
        List<ISysUser> dataList = projectRelUserDao.findUserListByProjectInfoId(params);
        PageInfo<ISysUser> pageInfo = new PageInfo<>(dataList);
        return pageInfo;
    }

    /**
     * 根据项目id获取未关联项目的用户分页列表
     *
     * @param pageNo              当前页
     * @param pageSize            当前页显示的数据量
     * @param param-sysUserName   用户名称
     * @param param-projectInfoId 项目主键
     */
    @Override
    public PageInfo<ISysUser> findNotRelateUserPage(Integer pageNo, Integer pageSize, Map param) {
        if (!StringUtil.checkObj(param.get("projectInfoId"))) {
            logger.warn("method[findNotRelateUserPage] projectInfoId is null.");
            return null;
        }
        if (!StringUtil.checkObj(pageNo)) {
            logger.warn("method[findNotRelateUserPage] pageNo is null. pageNo = 1");
            pageNo = 1;
        }
        if (!StringUtil.checkObj(pageSize)) {
            logger.warn("method[findNotRelateUserPage] pageSize is null. pageSize = 5");
            pageSize = 5;
        }
        PageHelper.startPage(pageNo, pageSize);
        List<ISysUser> dataList = projectRelUserDao.findNotRelateUserPage(param);
        PageInfo<ISysUser> pageInfo = new PageInfo<>(dataList);
        return pageInfo;
    }


    /**
     * 根据项目id获取关联关系
     *
     * @param projectInfoId 项目主键
     * @return
     */
    @Override
    public List<IProjectRelUser> findListByProjectInfoId(Long projectInfoId) {
        if (!StringUtil.checkObj(projectInfoId)) {
            return null;
        }
        Map param = new HashMap();
        param.put("projectInfoId", projectInfoId);
        return projectRelUserDao.findList(param);
    }

}
 



